home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / contrib / pgperl / pg-libpq.mus < prev    next >
Encoding:
Text File  |  1992-08-27  |  10.9 KB  |  540 lines

  1. /*
  2.  * Interface to Postgres 2.0
  3.  *
  4.  * $Header: /genome/src/postgres/src/contrib/pgperl/RCS/pg-libpq.mus,v 1.3 92/
  5. 01/30 16:50:35 hartzell Exp Locker: hartzell $
  6.  *
  7.  * Tue Aug 21 14:13:40 1990  Igor Metz <metz@iam.unibe.ch>
  8.  *
  9.  * $Id: pg-libpq.mus,v 1.3 1992/02/15 20:32:32 mer Exp $
  10.  * $Log: pg-libpq.mus,v $
  11. Revision 1.3  1992/02/15  20:32:32  mer
  12. updated by George Hartzell of the Stanford Genome project
  13.  
  14. # Revision 1.3  92/01/30  16:50:35  hartzell
  15. # added PQendcopy, PQgetline, and PQputline.  Don't trust
  16. # PQgetline, since it expects a pointer to a buffer (a char *) and
  17. # I don't really believe that perl is going to give it what it
  18. # wants.
  19. # Revision 1.2  92/01/30  16:18:33  hartzell
  20. # reworked to work with Postgres 3.1 and Perl 4.?.
  21. # Made sure that all PQ functions were available,
  22. # commented out all others, whether 'cuz they're out of date,
  23. # renamed, or not part of the public interface as defined
  24. # by Jeff Meredith (e.g. don't begin with PQ) varies.
  25. # Revision 1.1  1992/01/18  02:14:01  hartzell
  26. # Initial revision
  27. #
  28. Revision 1.2  91/03/08  13:22:32  kemnitz
  29. added RCS header.
  30.  
  31. Revision 1.1  90/10/24  20:31:11  cimarron
  32. Initial revision
  33.  
  34. Revision 1.1  90/08/23  15:18:20  metz
  35. Initial revision
  36.  
  37.  * 
  38.  */
  39.  
  40. #include "tmp/libpq.h"
  41. #include "tmp/libpq-fe.h"    /* superset of ones also in libpq-be.h */
  42.  
  43. #include "EXTERN.h"
  44. #include "perl.h"
  45.  
  46. extern char *PQhost;
  47. extern char *PQport;
  48. extern char *PQtty;
  49. extern char *PQoption;
  50. extern char *PQdatabase;
  51. extern int PQportset;
  52. extern int PQxactid;
  53. extern int PQtracep;
  54.  
  55. static enum uservars {
  56.   UV_PQhost,
  57.   UV_PQport,
  58.   UV_PQtty,
  59.   UV_PQoption,
  60.   UV_PQdatabase,
  61.   UV_PQportset,
  62.   UV_PQxactid,
  63.   UV_PQtracep
  64. };
  65.  
  66. static enum usersubs {
  67.   US_PQFlushI,
  68.   US_PQclear,
  69.   US_PQdb,
  70.   US_PQendcopy,
  71.   US_PQexec, 
  72.   US_PQfinish,
  73.   US_PQfn,
  74.   US_PQfname,
  75.   US_PQfnameGroup,
  76.   US_PQfnumber,
  77.   US_PQfnumberGroup,
  78.   US_PQftype,
  79.   US_PQgetgroup,
  80.   US_PQgetline,
  81.   US_PQgetvalue,
  82.   US_PQgroup,
  83.   US_PQnfields,
  84.   US_PQnfieldsGroup,
  85.   US_PQngroups,
  86.   US_PQninstances,
  87.   US_PQninstancesGroup,
  88.   US_PQnportals,
  89.   US_PQntuples,
  90.   US_PQntuplesGroup,
  91.   US_PQparray,
  92.   US_PQpnames,
  93.   US_PQputline,
  94.   US_PQreset,
  95.   US_PQrulep,
  96.   US_PQsametype,
  97.   US_PQsetdb,
  98.   US_PQtrace,
  99.   US_PQuntrace,
  100. };
  101.  
  102. static
  103. unsigned int
  104. dbl2uint(d)
  105.      double d;
  106. {
  107.   unsigned int i = d;
  108.   return i;
  109. }
  110.  
  111. static
  112. double
  113. uint2dbl(i)
  114.      unsigned int i;
  115. {
  116.   double d = i;
  117.   return d;
  118. }
  119.  
  120.  
  121. static int usersub();
  122. static int userset();
  123. static int userval();
  124.  
  125. extern void
  126. init_postgres_stuff()
  127. {    
  128.   struct ufuncs uf;
  129.   char *filename = "libpq.c";
  130.  
  131.   uf.uf_set = userset;
  132.   uf.uf_val = userval;
  133.  
  134. #define MAGICVAR(name, ix) uf.uf_index = ix, magicname(name, &uf, sizeof uf)
  135.  
  136.   /* register PG variables */
  137.   MAGICVAR("PQhost",        UV_PQhost);
  138.   MAGICVAR("PQport",        UV_PQport);
  139.   MAGICVAR("PQtty",        UV_PQtty);
  140.   MAGICVAR("PQoption",        UV_PQoption);
  141.   MAGICVAR("PQdatabase",    UV_PQdatabase);
  142.   MAGICVAR("PQportset",        UV_PQportset);
  143.   MAGICVAR("PQxactid",        UV_PQxactid);
  144.   MAGICVAR("PQtracep",        UV_PQtracep);
  145.  
  146.   /* register PG functions */
  147.   make_usub("PQFlushI",        US_PQFlushI,        usersub, filena
  148. me);
  149.   make_usub("PQclear",        US_PQclear,        usersub, filename);
  150.   make_usub("PQdb",        US_PQdb,        usersub, filename);
  151.   make_usub("PQendcopy",    US_PQendcopy,        usersub, filename);
  152.   make_usub("PQexec",        US_PQexec,        usersub, filename);
  153.   make_usub("PQfinish",        US_PQfinish,        usersub, filena
  154. me);
  155.   make_usub("PQfn",        US_PQfn,        usersub, filename);
  156.   make_usub("PQfname",        US_PQfname,        usersub, filename);
  157.   make_usub("PQfnameGroup",    US_PQfnameGroup,    usersub, filename);
  158.   make_usub("PQfnumber",    US_PQfnumber,        usersub, filename);
  159.   make_usub("PQfnumberGroup",    US_PQfnumberGroup,    usersub, filename);
  160.   make_usub("PQftype",        US_PQftype,        usersub, filename);
  161.   make_usub("PQgetgroup",    US_PQgetgroup,        usersub, filename);
  162.   make_usub("PQgetline",    US_PQgetline,        usersub, filename);
  163.   make_usub("PQgetvalue",    US_PQgetvalue,        usersub, filename);
  164.   make_usub("PQgroup",        US_PQgroup,        usersub, filename);
  165.   make_usub("PQnfields",    US_PQnfields,        usersub, filename);
  166.   make_usub("PQnfieldsGroup",    US_PQnfieldsGroup,    usersub, filename);
  167.   make_usub("PQngroups",    US_PQngroups,        usersub, filename);
  168.   make_usub("PQninstances",    US_PQninstances,    usersub, filename);
  169.   make_usub("PQninstancesGroup", US_PQninstancesGroup,  usersub, filename);
  170.   make_usub("PQnportals",    US_PQnportals,        usersub, filename);
  171.   make_usub("PQntuples",    US_PQntuples,        usersub, filename);
  172.   make_usub("PQntuplesGroup",    US_PQntuplesGroup,    usersub, filename);
  173.   make_usub("PQparray",        US_PQparray,        usersub, filena
  174. me);
  175.   make_usub("PQpnames",        US_PQpnames,        usersub, filena
  176. me);
  177.   make_usub("PQputline",    US_PQputline,        usersub, filename);
  178.   make_usub("PQreset",        US_PQreset,        usersub, filename);
  179.   make_usub("PQrulep",        US_PQrulep,        usersub, filename);
  180.   make_usub("PQsametype",    US_PQsametype,        usersub, filename);
  181.   make_usub("PQsetdb",        US_PQsetdb,        usersub, filename);
  182.   make_usub("PQtrace",        US_PQtrace,        usersub, filename);
  183.   make_usub("PQuntrace",    US_PQuntrace,        usersub, filename);
  184. }
  185.  
  186. static int
  187. usersub(ix, sp, items)
  188.      int ix;
  189.      register int sp;
  190.      register int items;
  191. {
  192.   STR **st = stack->ary_array + sp;
  193.   register int i;
  194.   register char *tmps;
  195.   register STR *Str;        /* used in str_get and str_gnum macros */
  196.  
  197.   switch (ix) {
  198. /*
  199.  * Used for flushing out "poll" queries by the monitor.
  200.  */
  201. CASE int PQFlushI
  202. I int i_count
  203. END
  204.  
  205. /*
  206.  * free storage claimed by named portal.
  207.  */
  208. CASE void PQclear
  209. I char* pname
  210. END
  211.  
  212. /*
  213.  * Return the current database being accessed.
  214.  */
  215. CASE char* PQdb
  216. END
  217.  
  218. /*
  219.  * returns 1 if all's well?
  220.  */
  221. CASE int PQendcopy
  222. END
  223.  
  224. /*
  225.  * Send a query to the POSTGRES backend.
  226.  * The return value is a string.
  227.  * If there is an error: return "E error-message".
  228.  * If tuples are fetched from the backend, return "P portal-name".
  229.  * If a query is executed successfully but no tuples fetched,
  230.  * return "C query-command".
  231.  */
  232. CASE char* PQexec 
  233. I  char* query
  234. END
  235.  
  236. /*
  237.  * Close communication ports with the backend.
  238.  */
  239. CASE void PQfinish 
  240. END
  241.  
  242. /*
  243.  * Send a function call to the POSTGRES backend.
  244.  *
  245.  * fnid         : function id
  246.  * result_buf   : pointer to result buffer (&int if integer)
  247.  * result_len   : length of return value.
  248.  * result_is_int: If the result is an integer, this must be non-zero
  249.  * args         : pointer to a NULL terminated arg array.
  250.  *                      (length, if integer, and result-pointer)
  251.  * nargs        : # of arguments in args array.
  252.  */
  253. CASE char* PQfn 
  254. I int         fnid
  255. I void*       result_buf
  256. I int         result_len
  257. I int         result_is_int
  258. I PQArgBlock* args
  259. I int         nargs
  260. END
  261.  
  262. /*
  263.  * Return the name of a field.
  264.  */
  265. CASE char* PQfname
  266. I PortalBuffer* portal
  267. I int           tuple_index
  268. I int         field_number
  269. END
  270.  
  271. /* Return the field (attribute) name given the group index
  272.  * and field index.
  273.  */
  274. CASE char* PQfnameGroup
  275. I PortalBuffer* portal
  276. I int           group_index
  277. I int           field_number
  278. END
  279.  
  280. /*
  281.  * Return the field index of a given field name within a tuple.
  282.  */
  283. CASE int PQfnumber
  284. I PortalBuffer* portal
  285. I int           tuple_index
  286. I char*         field_name
  287. END
  288.  
  289. /* Return the field number (index) given the group index and
  290.  * the field name.
  291.  */
  292. CASE int PQfnumberGroup
  293. I  PortalBuffer* portal
  294. I int            group_index
  295. I char*          field_name
  296. END
  297.  
  298. /*
  299.  * Return the type of a field.
  300.  */
  301. CASE int PQftype
  302. I PortalBuffer* portal
  303. I int           tuple_index
  304. I int         field_number
  305. END
  306.  
  307. /*
  308.  * Return the index of the group that a particular tuple is in.
  309.  */
  310. CASE int PQgetgroup
  311. I PortalBuffer* portal
  312. I int           tuple_index
  313. END
  314.  
  315. /* 
  316.  * USER FUNCTION - gets a newline-terminated string from the backend.
  317.  */
  318. CASE int PQgetline
  319. IO char* s
  320. I int    maxlen
  321. END
  322.  
  323. /* 
  324.  * Return an attribute (field) value.
  325.  */
  326. CASE char* PQgetvalue
  327. I PortalBuffer* portal
  328. I int           tuple_index
  329. I int         field_number
  330. END
  331.  
  332. /*
  333.  * Return the tuple group that a particular tuple is in.
  334.  */
  335. CASE GroupBuffer* PQgroup
  336. I PortalBuffer* portal
  337. I int           tuple_index
  338. END
  339.  
  340. /*
  341.  * Return the number of fields in a tuple.
  342.  */
  343. CASE int PQnfields
  344. I PortalBuffer* portal
  345. I int           tuple_index
  346. END
  347.  
  348. /*
  349.  * Return the number of fields in a tuple group.
  350.  */
  351. CASE int PQnfieldsGroup
  352. I  PortalBuffer* portal
  353. I int            group_index
  354. END
  355.  
  356. /*
  357.  * Return the number of tuple groups in a portal buffer.
  358.  */
  359. CASE int PQngroups
  360. I  PortalBuffer* portal
  361. END
  362.  
  363. /*
  364.  *
  365.  */
  366. CASE int PQninstances
  367. I PortalBuffer* portal
  368. END
  369.  
  370. /*
  371.  *
  372.  */
  373. CASE int PQninstancesGroup
  374. I PortalBuffer* portal
  375. I int           group_index
  376. END
  377.  
  378. /* 
  379.  * Return the number of open portals. 
  380.  * If rule_p, only return asynchronized portals. 
  381.  */
  382. CASE int PQnportals
  383. I int rule_p
  384. END
  385.  
  386. /*
  387.  * Return the number of tuples in a portal buffer.
  388.  */
  389. CASE int PQntuples
  390. I  PortalBuffer* portal
  391. END
  392.  
  393. /*
  394.  * Return the number of tuples in a tuple group.
  395.  */
  396. CASE int PQntuplesGroup
  397. I  PortalBuffer* portal
  398. I int            group_index
  399. END
  400.  
  401. /*
  402.  * Return the portal buffer given a portal name.
  403.  */
  404. CASE PortalBuffer* PQparray
  405. I char* pname
  406. END
  407.  
  408. /* 
  409.  * Return all the portal names.
  410.  * If rule_p, only return asynchronized portals. 
  411.  */
  412. CASE void PQpnames
  413. I char** pnames
  414. I int    rule_p
  415. END
  416.  
  417. /*
  418.  * USER FUNCTION - sends a string to the backend.
  419.  */
  420. CASE int PQputline
  421. I char* s
  422. END
  423.  
  424. /*
  425.  * Reset the communication port with the backend.
  426.  */
  427. CASE void PQreset 
  428. END
  429.  
  430. /*
  431.  * Return 1 if an asynchronized portal.
  432.  */
  433. CASE int PQrulep
  434. I  PortalBuffer* portal
  435. END
  436.  
  437. /* Return 1 if the two tuples have the same type (in the
  438.  * same group).
  439.  */
  440. CASE int PQsametype
  441. I PortalBuffer* portal
  442. I int           tuple_index1
  443. I int           tuple_index2
  444. END
  445.  
  446. /*
  447.  * Make the specified database the current database.
  448.  */
  449. CASE void PQsetdb 
  450. I  char* dbname
  451. END
  452.  
  453. /*
  454.  * turn on pqdebug() tracing.
  455.  */
  456. CASE void PQtrace 
  457. END
  458.  
  459. /*
  460.  * turn off pqdebug() tracing.
  461.  */
  462. CASE void PQuntrace 
  463. END
  464.  
  465.   default:
  466.     fatal("Unimplemented user-defined subroutine");
  467.   }
  468.   return sp;
  469. }
  470.  
  471. static int
  472. userval(ix, str)
  473.      int ix;
  474.      STR *str;
  475. {
  476.   switch (ix) {
  477.   case UV_PQhost:
  478.     str_set(str, PQhost);
  479.     break;
  480.   case UV_PQport:
  481.     str_set(str, PQport);
  482.     break;
  483.   case UV_PQtty:
  484.     str_set(str, PQtty);
  485.     break;
  486.   case UV_PQoption:
  487.     str_set(str, PQoption);
  488.     break;
  489.   case UV_PQdatabase:
  490.     str_set(str, PQdatabase);
  491.     break;
  492.   case UV_PQportset:
  493.     str_numset(str, PQportset);
  494.     break;
  495.   case UV_PQxactid:
  496.     str_numset(str, PQxactid);
  497.     break;
  498.   case UV_PQtracep:
  499.     str_numset(str, PQtracep);
  500.     break;
  501.   }
  502.   return 0;
  503. }
  504.  
  505. static int
  506. userset(ix, str)
  507.      int ix;
  508.      STR *str;
  509. {
  510.   switch (ix) {
  511.   case UV_PQhost:
  512.     strcpy(PQhost, str_get(str));        /* hope it fits */
  513.     break;
  514.   case UV_PQport:
  515.     strcpy(PQport, str_get(str));        /* hope it fits */
  516.     break;
  517.   case UV_PQtty:
  518.     strcpy(PQtty, str_get(str));        /* hope it fits */
  519.     break;
  520.   case UV_PQoption:
  521.     strcpy(PQoption, str_get(str));        /* hope it fits */
  522.     break;
  523.   case UV_PQdatabase:
  524.     strcpy(PQdatabase, str_get(str));        /* hope it fits */
  525.     break;
  526.   case UV_PQportset:
  527.     PQportset = dbl2uint(str_gnum(str));
  528.     break;
  529.   case UV_PQxactid:
  530.     PQxactid = dbl2uint(str_gnum(str));
  531.     break;
  532.   case UV_PQtracep:
  533.     PQtracep = dbl2uint(str_gnum(str));
  534.     break;
  535.   }
  536.   return 0;
  537. }
  538.